package com.kimehwa.studyboot.elasticsearchtest;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author xiaodongguan
 * @description
 * @since 2021-09-21 14:47
 */

public class EstestInsert {
    /*
    增删改查都是要他进行搞起
     */
    public static void main(String[] args) throws IOException {

        // 创建es对象
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("47.105.163.178",9200,"http"))
        );

        IndexRequest indexRequest = new IndexRequest();
        indexRequest.index("user").id("1001");
        User build = User.builder().age(18).sex("famale").name("lihua").build();
        String jsonString = JSON.toJSONString(build);

        indexRequest.source(jsonString, XContentType.JSON);

        IndexResponse response = esClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(response.getResult());


        esClient.close();



    }
}
